package de.backtomedieval.units;


//position @ Array
//Archer // Short // Long // 2-Hand // Spear // Hallberdier // Hoplit // Ballista // Siege - Ram // Catapult // Trebuchet // Tower

import java.util.ArrayList;
import java.util.Random;

import de.backtomedieval.R;
import de.backtomedieval.game.City;
import de.backtomedieval.game.Costs;
import de.backtomedieval.research.Technology;
import de.backtomedieval.scenarios.AbstractScenario;
import de.backtomedieval.scenarios.DefendYourCityAgainst10Longswordmen;
import de.backtomedieval.scenarios.DefendYourCityAgainst5Archers;
import de.backtomedieval.scenarios.Defend_GreatSiege;
import de.backtomedieval.scenarios.Defend_EvenGreaterSiege;

public abstract class AbstractUnit {

	public static final int BUILD_IN_BARRACKS = 0;
	public static final int BUILD_IN_MANUFRACTURE = 1;
	
	
	private int healthpoints = 0;
	private int attack = 0;
	private int attackMultiple = 0;
	private int defense = 0;
	private int attackquality = 0; //from 0 to 100 in %
	private int[] attackRounds = {};
	private Costs costs = null;
	private int buildCountdown = 0;
	
	abstract public String getName(int count);
	public String getName() {
		return getName(1);
	}
	abstract public int getBuildIn();
	
	public int getHealthpoints() {
		return healthpoints;
	}
	public void setHealthpoints(int healthpoints) {
		this.healthpoints = healthpoints;
	}
	public int getAttack() {
		return attack;
	}
	public void setAttack(int attack) {
		this.attack = attack;
	}
	
	public int getAttackMultiple() {
		return attackMultiple;
	}
	public void setAttackMultiple(int attackMultiple) {
		this.attackMultiple = attackMultiple;
	}
	public int getDefense() {
		return defense;
	}
	public void setDefense(int defense) {
		this.defense = defense;
	}
	public int getAttackquality() {
		return attackquality;
	}
	public void setAttackquality(int attackquality) {
		this.attackquality = attackquality;
	}
	public int[] getAttackRounds() {
		return attackRounds;
	}
	public void setAttackRounds(int[] attackRounds) {
		this.attackRounds = attackRounds;
	}

	public Costs getCosts() {
		return costs;
	}
	public void setCosts(Costs costs) {
		this.costs = costs;
	}
	

	
	
	//-----------
	
	AbstractUnit(int healthpoints, int attack, int defense, int attackquality, int attackMultiple, int[] attackRounds, int buildCountdown) {
		this.healthpoints = healthpoints;
		this.attack = attack;
		this.defense = defense;
		this.attackquality = attackquality;
		this.attackMultiple = attackMultiple;
		this.attackRounds = attackRounds;
		this.setBuildCountdown(buildCountdown);
	}
	
	int hurt(int damage) {
		if (healthpoints < damage) {
			int lostHealthpoints = healthpoints;
			healthpoints = 0;
			return lostHealthpoints;
		}
		healthpoints = healthpoints - damage;
		return damage;
	}
	
	int attack(AbstractUnit enemy) {
		int damage = this.attack - enemy.defense;
		if (damage < 1) {damage = 1;}
		Random ra = new Random();
		int testAttackquality;
		testAttackquality = ra.nextInt(100) +1;
		if (testAttackquality <= this.attackquality) {
			//Probe auf Angriffswahrscheinlichkeit gelingt -> Angriff
			enemy.hurt(damage);
			return damage;
		} else {
			//Probe auf Angriffswahrscheinlichkeit missling -> Geblockter Angriffsversuch/Daneben
			return 0;
		}
	}
	
	int attack(AbstractUnit enemy, City city) {
		int attacking = this.attack;
		if (city!=null && city.getDefence_wall().getHealthpoints()>0) {
			int cityattacking;
			attacking = (int) (attacking * 0.5);
			cityattacking = this.attack - attacking;
			cityattacking = cityattacking - city.getDefence_wall().getDefense();
			if (cityattacking < 1) {cityattacking = 1;}
			city.getDefence_wall().hurt(cityattacking);
		}
		int damage = (attacking) - enemy.defense;
		if (damage < 1) {damage = 1;}
		Random ra = new Random();
		int testAttackquality;
		testAttackquality = ra.nextInt(100) +1;
		if (testAttackquality <= this.attackquality) {
			//Probe auf Angriffswahrscheinlichkeit gelingt -> Angriff
			enemy.hurt(damage);
			return damage;
		} else {
			//Probe auf Angriffswahrscheinlichkeit missling -> Geblockter Angriffsversuch/Daneben
			return 0;
		}
	}
	

	
	boolean isAlive() {
		if (healthpoints > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	boolean canAttack(int round) {

		for (int i=0; i<attackRounds.length;i++) {
			if (attackRounds[i] == round) {
				return true;
			}
		}
		return false;
	}
	

	
	public static ArrayList<AbstractUnit> getUnitList(Technology technology) {
		ArrayList<AbstractUnit> u = new ArrayList<AbstractUnit>();
		u.add(new Archer(technology));
		u.add(new Spearman(technology));
		u.add(new ShortSwordsman(technology));
		u.add(new LongSwordsman(technology));
		u.add(new TwoHandedSwordman(technology));
		u.add(new Hoplit(technology));
		u.add(new Halberdier(technology));
		u.add(new SiegeRam(technology));
		u.add(new Ballista(technology));
		u.add(new Catapult(technology));
		u.add(new Trebuchet(technology));
		u.add(new Tower(technology));
		return u;
	}
	
	
	public Integer getImage() {
		return R.drawable.icon;
	}
	
	public String getAttackroundString() {
		String tmp ="";
		int lowest=-1;
		int last = -1;
		for (int i=0; i<attackRounds.length;i++) {
			tmp += attackRounds[i] + " ";
			if (last==-1) {
				lowest=attackRounds[i];
				last=attackRounds[i];
			} else if(last==-2) {
			} else {
				if (attackRounds[i]==(last+1)) {
					last = attackRounds[i];
				} else {
					last = -2;
				}
			}
		}
		if (last!=-1 && last!=-2) {
			return lowest +"-" +last;
		}
		return tmp;
	}
	public void setBuildCountdown(int buildCountdown) {
		this.buildCountdown = buildCountdown;
	}
	public int getBuildCountdown() {
		return buildCountdown;
	}
	


}
